package org.eae.eldf.framework.redis;

import org.eae.eldf.framework.redis.serializer.ObjectRedisSerializer;
import org.springframework.data.redis.connection.DefaultStringRedisConnection;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
 * 此bean只能在service中使用 ，并且该service是有事物的
 * @author fxm
 *
 */
public class TranscationObjectRedisTemplate extends RedisTemplate<String, Object>{

	public TranscationObjectRedisTemplate(){
		RedisSerializer<Object> objectSerializer = new ObjectRedisSerializer();
		//JdkSerializationRedisSerializer objectSerializer = new JdkSerializationRedisSerializer();
		setKeySerializer(new StringRedisSerializer());
		setValueSerializer(objectSerializer);
		setHashKeySerializer(new StringRedisSerializer());
		setHashValueSerializer(objectSerializer);
		setEnableTransactionSupport(true);
	}
	
	/**
	 * Constructs a new <code>StringRedisTemplate</code> instance ready to be used. 
	 *
	 * @param connectionFactory connection factory for creating new connections
	 */
	public TranscationObjectRedisTemplate(RedisConnectionFactory connectionFactory) {
		this();
		setConnectionFactory(connectionFactory);
		afterPropertiesSet();
	}
	
	protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) {
		return new DefaultStringRedisConnection(connection);
	}
	
}